from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import time

def cosine_similarity_local(a, b):
    dot_product = np.dot(a, b)
    norm_a = np.linalg.norm(a)
    norm_b = np.linalg.norm(b)
    return dot_product / (norm_a * norm_b)

# 示例向量
vec_a = np.array([0.35, 4.67, 3.58, 123.34, 45.33])
vec_b = np.array([0.35, 4.67, 3.58, 123.34, 43.33])

print(f"vec_a: {vec_a}")
print(f"vec_b: {vec_b}")

start_time = time.time()
similarity = cosine_similarity_local(vec_a, vec_b)
print(f"余弦相似度 local: {similarity:.4f}, 执行时间： {time.time() - start_time:.4f}秒")

start_time = time.time()
similarity = cosine_similarity([vec_a], [vec_b])
print(f"余弦相似度 lib: {similarity[0][0]:.4f}, 执行时间： {time.time() - start_time:.4f}秒")
